package com.vm.osm.business.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.vm.osm.business.common.Constants;

import java.io.File;
import java.util.Random;
import java.util.UUID;

/**七牛云相关操作
 * Created by zhangshilin on 2017/4/23.
 */
public class QiniuUtils {

    private static final String QINIU_ACCESS_KEY = Constants.QINIU_ACCESS_KEY;
    private static final String QINIU_SECRET_KEY = Constants.QINIU_SECRET_KEY;
    public static final String QINIU_URL=Constants.QINIU_URL;
//    private static final String QINIU_BUCKET = "hhyc-video";
    private static final Zone QINIU_ZONE = Constants.QINIU_ZONE;
    private static final long EXPIRE_SECONDS = Constants.EXPIRE_SECONDS;


    /**
     * 获取七牛上传凭证
     * @return
     */
    public static String getQiNiuToken(String buket){
        Auth auth = Auth.create(QINIU_ACCESS_KEY, QINIU_SECRET_KEY);
        StringMap putPolicy = new StringMap();
        //putPolicy.put("returnBody", "{\"key\":\"$(key)\",\"hash\":\"$(etag)\",\"bucket\":\"$(bucket)\",\"fsize\":$(fsize),\"user\":\"$(x:user)\"");
        String upToken = auth.uploadToken(buket,null,EXPIRE_SECONDS,null);
        return upToken;
    }

    /**
     *
     * @param upToken
     * @param localFilePath 本地文件路径
     * @return 七牛文件名称
     */
    public static String upFileToQiNiuZone(String upToken,String localFilePath){
        try{
            //System.out.println("开始上传七牛视频");
            Configuration cfg = new Configuration(QINIU_ZONE);
            UploadManager uploadManager = new UploadManager(cfg);
            String str = "img"+UUID.randomUUID().toString().replaceAll("-", "") + new Random().nextInt(10);
    		
            Response response = uploadManager.put(localFilePath, str, upToken);
            //解析上传成功的结果
            //System.out.println(response.bodyString());
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);

            //System.out.println("上传七牛视频成功,key = " + putRet.key + " hash = " + putRet.hash);
            //System.out.println(putRet.key);
            //System.out.println(putRet.hash);
            File localFile=new File(localFilePath);
            localFile.delete();

            return putRet.key;
        }catch (QiniuException ex){
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }
        return "";
    }

    /**
     * 删除资源文件
     * @param bucket
     * @param fileName
     */
    public static boolean deteteBucketFile(String bucket,String fileName){
        Configuration cfg = new Configuration(QINIU_ZONE);
        Auth auth = Auth.create(QINIU_ACCESS_KEY,QINIU_SECRET_KEY);
        BucketManager bucketManager = new BucketManager(auth,cfg);
        try {
            bucketManager.delete(bucket,fileName);
        }catch (QiniuException ex){
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
            return  false;
        }

        return true;

    }


    public static void main(String[] args){
//        String bucket = Constants.QINIU_VIDEO_BUKET;
        String bucket = "video-nengku";
        String upToken = getQiNiuToken(bucket);
        String localFilePath = "/Users/chenzui/Documents/成都天府新区.png";
        //System.out.println(upToken);
        String res = upFileToQiNiuZone(upToken,localFilePath);
       // System.out.println(res);
//        Auth auth = Auth.create(QINIU_ACCESS_KEY,QINIU_SECRET_KEY);
//        deteteBucketFile(bucket, "11.png");

    }



}
